using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;
using WorkOrder.Models;

namespace WorkSystem.Data
{
    public class NewTicketView
    {
        public string 工单标题 { get; set; } = "新的工单";

        [DisplayName("工单内容")]
        [Required(ErrorMessage = "工单内容不能为空,必须填写表单")]
        public string 工单内容 { get; set; } = null!;

        [Required(ErrorMessage = "必须选择优先级")]
        public PriorityLevel 优先级 { get; set; } = PriorityLevel.正常;

        [Required(ErrorMessage = "必须选择模板")]
        public string 模板ID { get; set; }

        public Dictionary<string, object> 扩展字段 { get; set; } = new();

        [Required(ErrorMessage = "必须选择流程")]
        public string 流程ID { get; set; } = string.Empty;

        /// <summary>
        /// Converts 工单内容 into 扩展字段.
        /// </summary>
        public void ConvertContentToExtendedFields()
        {
            if (!string.IsNullOrWhiteSpace(工单内容))
            {
                // Example logic: Split 工单内容 by a delimiter (e.g., ";") and populate 扩展字段
                var lines = 工单内容.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
               
                foreach (var line in lines)
                {
                    
                    var parts = line.Split(':', 2);
                    if (parts.Length == 2)
                    {
                        var fieldDisplayName = parts[0].Trim();
                        var fieldValue = parts[1].Trim();
                        扩展字段[fieldDisplayName] = fieldValue;
        
                    }
                    else
                    {
                        throw new InvalidOperationException($"无法解析行: {line}");
         
                    }
                }

                if (!扩展字段.Any())
                {
                       throw new InvalidOperationException($"未能解析任何字段，请检查粘贴内容的格式");


                }
            }
        }
    }
}